# -*- coding :utf-8 -*-
import psutil
import time
import datetime
import tkinter as tk
from tkinter import ttk
import threading
import os
import psutil

def Real_Time_Monitoring():
    launch_button['state'] = 'disabled'
    time.sleep(3)
    lb.insert(tk.END, "启动完成")
    while 1:
        cpu_usage_int = psutil.cpu_percent(interval=1)
        memory_usage_int = psutil.virtual_memory().percent
        current_time_int = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        time_display.configure(text=current_time_int)
        cpu_usage_width = cpu_usage_int / 100 * 200
        cpu_usage.coords(cpu_usage_used, 0, 0, cpu_usage_width, 20)
        cpu_display.configure(text=f"{cpu_usage_int}% / 100%")

        memory_usage_width = memory_usage_int / 100 * 200
        memory_usage.coords(memory_usage_used, 0, 0, memory_usage_width, 20)
        memory_display.configure(text=f"{memory_usage_int}% / 100%")
        time.sleep(0.5)
def update_listbox():
    if Library.get() == 1:
        lb.insert(tk.END, "正在安装新的库......")
        os.system("cnpm i")
    lb.insert(tk.END, "正在启动......")
    global launch_flag
    launch_flag = 1
    #创建线程
    threading.Thread(name="Real_Time_Monitoring",target=Real_Time_Monitoring).start()
    os.system("cnpm run dev")
def launch():
    # 启动定时更新Listbox的线程
    threading.Thread(name="update_listbox",target=update_listbox).start()
    print('hello world')
# 创建Tkinter窗口
window = tk.Tk()
window.title("系统监控")
window.geometry('750x280')


# 创建启动设置区域
launch_settings_frame = tk.Frame(window)
launch_settings_frame.pack()
# 创建启动按钮
launch_button = tk.Button(launch_settings_frame, text='启动',command=launch, width=10)
launch_button.pack()
# 创建启动选项
Library = tk.IntVar()
Library_check = tk.Checkbutton(launch_settings_frame, text='安装库', variable=Library)
Library_check.pack(side='left')

# 创建Scrollbar(滚动条)和Listbox(列表)
lb = tk.Listbox(window, width=50)
lb.pack(side="left", fill="both")
sb = tk.Scrollbar(window)
sb.pack(side="right", fill="y")
lb.config(yscrollcommand=sb.set)
sb.config(command=lb.yview)

# 创建资源监控区域
resource_monitoring_frame = tk.Frame(window)
resource_monitoring_frame.pack()
# 创建资源监控标签（即内存与cpu）
time_label = tk.Label(resource_monitoring_frame, text="时间:")
time_label.pack(side="left")
time_display = tk.Label(resource_monitoring_frame, text="")
time_display.pack(side="left")

resource_monitoring_frame = tk.Frame(window)
resource_monitoring_frame.pack()

cpu_label = tk.Label(resource_monitoring_frame, text="CPU使用率:")
cpu_label.pack(side="left")
cpu_usage = tk.Canvas(resource_monitoring_frame, width=200, height=20, bg="#C0C0C0", highlightthickness=0)
cpu_usage.pack(side="left")
cpu_usage_used = cpu_usage.create_rectangle(0, 0, 20, 0, fill="#75FA61",width=0)
cpu_display = tk.Label(resource_monitoring_frame, text="0% / 100%")
cpu_display.pack(side="left")

resource_monitoring_frame = tk.Frame(window)
resource_monitoring_frame.pack()

memory_label = tk.Label(resource_monitoring_frame, text="内存使用率:")
memory_label.pack(side="left")
memory_usage = tk.Canvas(resource_monitoring_frame, width=200, height=20, bg="#C0C0C0", highlightthickness=0)
memory_usage.pack(side="left")
memory_usage_used = memory_usage.create_rectangle(0, 0, 20, 0, fill="#75FA61",width=0)
memory_display = tk.Label(resource_monitoring_frame, text="0% / 100%")
memory_display.pack(side="left")

window.mainloop()